package com.yx.pc.api.controller;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.entity.*;
import com.yx.pc.api.Dto.TbInterviewCommentDto;
import com.yx.pc.api.service.PcTbCommentDianzanRecordService;
import com.yx.pc.api.service.PcTbInterviewClickRecordService;
import com.yx.pc.api.service.PcTbInterviewCommentService;
import com.yx.pc.api.service.PcTbInterviewService;
import org.apache.poi.hssf.record.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 访谈
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-14 15:00:09
 */
@RestController
@RequestMapping("/pc/api/interview")
public class PcTbInterviewController {
	@Autowired
	private PcTbInterviewService interviewService;

	@Autowired
    private PcTbInterviewClickRecordService interviewClickRecordService;

	@Autowired
	private PcTbInterviewCommentService pcTbInterviewCommentService;

	@Autowired
	private PcTbCommentDianzanRecordService tbCommentDianzanRecordService;

	/**
	 * 列表
	 */
	@AuthIgnore
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);


		List<TbInterview> interviewList = interviewService.queryList(query);
		int total = interviewService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(interviewList, total, query.getLimit(), query.getPage());

		return R.ok().put("data", pageUtil);
	}


	/*
	* 点赞访谈
	*/
	@RequestMapping("/dianzan")
	public R dianzan(@LoginUser TbUser tbUser,Integer id){
        //首先根据用户id和访谈id，去查询记录。
        TbInterviewClickRecord tbInterviewClickRecord = interviewClickRecordService.findByUserAnd(tbUser.getUserId(),id);
        //判断访谈详情
        if (tbInterviewClickRecord==null){
            //查询访谈
            TbInterview tbInterview = interviewService.queryObject(id);
            if (tbInterview==null){
                return R.error("访谈已经消失啦");
            }
            //创建记录保存
            TbInterviewClickRecord tbInterviewClickRecord1 = new TbInterviewClickRecord();
            tbInterviewClickRecord1.setUserId(tbUser.getUserId());
            tbInterviewClickRecord1.setInterviewId(id);
            tbInterviewClickRecord1.setDate(new Date());
            interviewClickRecordService.save(tbInterviewClickRecord1);
            //增加点赞数,更新数据
            tbInterview.setLikeCount(tbInterview.getClickCount()+1);
            interviewService.update(tbInterview);
            return R.ok();
        }else {
            return R.error("请勿重复点赞");
        }
	}


	/**
	 * 查看访谈对应的访谈评论
	 */
	@AuthIgnore
	@RequestMapping("/commentList")
	public R commentList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		//首先查找图书下的所有图书评论
		List<TbInterviewComment> interviewCommentList = pcTbInterviewCommentService.queryList(query);
		int total = pcTbInterviewCommentService.queryTotal(query);
		//初始化数据传输对象集合
		List<TbInterviewCommentDto> tbInterviewCommentDtos = new ArrayList<>();
		//循环所有的图书评论
		for (TbInterviewComment tbInterviewComment:interviewCommentList){
			//初始化数据传输对象
			TbInterviewCommentDto tbInterviewCommentDto = new TbInterviewCommentDto();
			//查询图书评论下的所有回复评论
			List<TbInterviewComment> tbInterviewComments = pcTbInterviewCommentService.findByReplyFristId(tbInterviewComment.getId());
			//注入数据
			tbInterviewCommentDto.setTbInterviewComment(tbInterviewComment);
			tbInterviewCommentDto.setTbInterviewComments(tbInterviewComments);
			//加入集合
			tbInterviewCommentDtos.add(tbInterviewCommentDto);
		}

		PageUtils pageUtil = new PageUtils(tbInterviewCommentDtos, total, query.getLimit(), query.getPage());

		return R.ok().put("data", pageUtil);
	}


	/*
	*评论访谈
	*/
	@RequestMapping("/commentInterview")
	public R commentBook(@LoginUser TbUser tbUser,Integer interviewId,String content){
		if (tbUser!=null){
			TbInterviewComment interviewComment = new TbInterviewComment();
			interviewComment.setContent(content);
			interviewComment.setDate(new Date());
			interviewComment.setInterviewId(interviewId);
			interviewComment.setLikeCount(0);
			interviewComment.setReplyCount(0);
			interviewComment.setType(0);
			interviewComment.setUserName(tbUser.getNickname());
			interviewComment.setUserImg(tbUser.getProfileimageurl());
			interviewComment.setUserId(tbUser.getUserId());
			pcTbInterviewCommentService.save(interviewComment);
			//找到对应访谈增加评论数
			TbInterview tbInterview = interviewService.queryObject(interviewId);
			tbInterview.setCommentCount(tbInterview.getCommentCount()+1);
			//更新访谈
			interviewService.update(tbInterview);
		}else {
			return R.error("用户不存在");
		}
		return R.ok();
	}

	/*
	*回复某条评论
	*/
	@RequestMapping("/replyComment")
	public R replyComment(@LoginUser TbUser tbUser,Integer id,String content){
		//根据id去查找评论
		TbInterviewComment tbInterviewComment1 = pcTbInterviewCommentService.queryObject(id);
		if (tbInterviewComment1==null){
			return R.error("评论已经不存在啦");
		}
		//初始化回复评论对象
		TbInterviewComment tbInterviewComment = null;
		//如果回复的评论是图书评论
		if (tbInterviewComment1.getType()==0){
			tbInterviewComment = new TbInterviewComment();
			tbInterviewComment.setUserId(tbUser.getUserId());
			tbInterviewComment.setUserImg(tbUser.getProfileimageurl());
			tbInterviewComment.setUserName(tbUser.getNickname());
			tbInterviewComment.setInterviewId(tbInterviewComment1.getInterviewId());
			tbInterviewComment.setContent(content);
			tbInterviewComment.setDate(new Date());
			tbInterviewComment.setType(1);
			tbInterviewComment.setReplyId(tbInterviewComment1.getId());
			tbInterviewComment.setReplyFristId(tbInterviewComment1.getId());
			tbInterviewComment.setReplyUserId(tbInterviewComment1.getUserId());
			tbInterviewComment.setReplyUserName(tbInterviewComment1.getUserName());
			//插入数据
			pcTbInterviewCommentService.save(tbInterviewComment);
			//增加图书评论的回复数
			tbInterviewComment1.setReplyCount(tbInterviewComment1.getReplyCount()+1);
			//更新
			pcTbInterviewCommentService.update(tbInterviewComment1);
			return R.ok();
		}
		//如果回复的评论是回复评论
		if (tbInterviewComment1.getType()==1){
			tbInterviewComment = new TbInterviewComment();
			tbInterviewComment.setUserId(tbUser.getUserId());
			tbInterviewComment.setUserImg(tbUser.getProfileimageurl());
			tbInterviewComment.setUserName(tbUser.getNickname());
			tbInterviewComment.setInterviewId(tbInterviewComment1.getInterviewId());
			tbInterviewComment.setContent(content);
			tbInterviewComment.setDate(new Date());
			tbInterviewComment.setType(1);
			tbInterviewComment.setReplyId(tbInterviewComment1.getId());
			tbInterviewComment.setReplyFristId(tbInterviewComment1.getReplyFristId());
			tbInterviewComment.setReplyUserId(tbInterviewComment1.getUserId());
			tbInterviewComment.setReplyUserName(tbInterviewComment1.getUserName());
			//插入数据
			pcTbInterviewCommentService.save(tbInterviewComment);
			return R.ok();
		}
		return R.ok();
	}


	/*
	* 点赞
	*/
	@RequestMapping("/clickPraise")
	public R clickPraise(@LoginUser TbUser tbUser,Integer id){
		//根据id找到评论
		TbInterviewComment  tbInterviewComment= pcTbInterviewCommentService.queryObject(id);
		if (tbInterviewComment==null){
			return R.error("查无此评论");
		}
		//判读评论是否图书评论
		if (tbInterviewComment.getType()==0){
			//查询评论点赞记录
			TbCommentDianzanRecord tbCommentDianzanRecord = tbCommentDianzanRecordService.findInterviewRcord(tbUser.getUserId(),tbInterviewComment.getId());
			//如果空，则可以点赞，如果非空进行提示
			if (tbCommentDianzanRecord==null){
				//增加点赞数
				tbInterviewComment.setLikeCount(tbInterviewComment.getLikeCount()+1);
				pcTbInterviewCommentService.update(tbInterviewComment);
				//创建图书评论点赞记录，并保存
				TbCommentDianzanRecord tbCommentDianzanRecord1 = new TbCommentDianzanRecord();
				tbCommentDianzanRecord1.setUserId(tbUser.getUserId());
				tbCommentDianzanRecord1.setCommentId(tbInterviewComment.getId());
				tbCommentDianzanRecord1.setCreateDate(new Date());
				tbCommentDianzanRecord1.setType(1);
				tbCommentDianzanRecordService.save(tbCommentDianzanRecord1);
				return R.ok();
			}else {
				return R.error("请勿重复点赞");
			}
		}else {
			return R.error("请选择图书评论点赞");
		}
	}
}
